package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.CurriculumCategoryDTO;
import com.fuli.app.dto.ImageDTO;
import com.fuli.app.service.CurriculumCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Api(tags = "课程分类")
@RestController
@RequestMapping("/curriculumCategory")
public class CurriculumCategoryController {
    @Autowired
    private CurriculumCategoryService curriculumCategoryService;

    public static final long MAX_SIZE = 1024 * 512;
    public static final Set<String> IMAGE_SUFFIX_SET = new HashSet<>(Arrays.asList(
            "jpg", "JPG", "jpge", "JPGE", "png", "PNG", "gif", "GIF"));


    @ApiOperation(
            value = "新增课程分类",
            notes = "<b>必传属性：</b><br/>" +
                    "1.name 课程分类名称<br/>" +
                    "2.leaf 是否是叶子节点<br/>" +
                    "<i>根节点parentId = root</i>")
    @PostMapping(value = "/add")
    public ActionResult<CurriculumCategoryDTO> add(@ApiParam("图片") @RequestParam(value = "image", required = false) MultipartFile file,
                                                   @ApiParam("课程分类名称") @RequestParam String name,
                                                   @ApiParam("父节点id") @RequestParam String parentId,
                                                   @ApiParam("课程分类简介") @RequestParam(value = "intro", required = false) String intro,
                                                   @ApiParam("是否是叶子节点") @RequestParam Boolean leaf) {
        if (file != null) {
            if (file.getSize() > MAX_SIZE)
                return new ActionResult<>(0, "图片大小超过限制");
            if (file.isEmpty())
                return new ActionResult<>(0, "文件不存在");
            int lastIndex = file.getOriginalFilename().lastIndexOf('.');
            if (lastIndex == -1)
                return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
            String suffix = file.getOriginalFilename().substring(lastIndex + 1);
            if (StringUtils.isEmpty(suffix))
                return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
            if (!IMAGE_SUFFIX_SET.contains(suffix))
                return new ActionResult<>(0, "文件格式不支持，仅支持jpg,jpge,png,gif格式");
        }
        if (StringUtils.isEmpty(name))
            return new ActionResult<>(0, "课程分类名称为空");
        if (StringUtils.isEmpty(parentId))
            if (leaf == null)
                return new ActionResult<>(0, "叶子节点属性值为空");

        return curriculumCategoryService.add(file, name, parentId, intro, leaf);
    }

    @ApiOperation(value = "删除课程分类")
    @DeleteMapping(value = "/delete/{id}")
    public ActionResult<String> delete(@ApiParam("课程分类id") @PathVariable String id) {
        return curriculumCategoryService.delete(id);
    }

    @ApiOperation(
            value = "修改课程分类",
            notes = "<b>必传属性：</b><br/>" +
                    "1.id 主键<br/>" +
                    "2.name 课程分类名称<br/>" +
                    "<i>leaf 叶子节点属性不可以修改</i><br/>" +
                    "<i>根节点parentId = root</i>")
    @PutMapping(value = "/update")
    public ActionResult<String> update(@ApiParam("课程分类对象") @RequestBody CurriculumCategoryDTO dto) {
        /*验证数据*/
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getId()))
            errors.add(new ErrorInfo(0, "id为空"));
        if (StringUtils.isEmpty(dto.getName()))
            errors.add(new ErrorInfo(0, "名称为空"));
        if (StringUtils.isEmpty(dto.getParentId()))
            errors.add(new ErrorInfo(0, "父节点不能为空"));
        if (!errors.isEmpty())
            return new ActionResult<>(errors);
        return curriculumCategoryService.update(dto);
    }

    @ApiOperation(value = "获取课程分类")
    @GetMapping(value = "/get/{id}")
    public ActionResult<CurriculumCategoryDTO> get(@ApiParam("课程分类id") @PathVariable String id) {
        return curriculumCategoryService.get(id);
    }

    @ApiOperation("升序")
    @PutMapping("/up/{id}")
    public ActionResult<String> up(@ApiParam("id") @PathVariable String id) {
        return curriculumCategoryService.up(id);
    }

    @ApiOperation("降序")
    @PutMapping("/down/{id}")
    public ActionResult<String> down(@ApiParam("id") @PathVariable String id) {
        return curriculumCategoryService.down(id);
    }

    @ApiOperation(value = "查询指定父节点id下的所有分类", notes = "parentId = null 查询所有分类，parentId = root 查询顶级分类")
    @GetMapping(value = "/list")
    public ActionResult<List<CurriculumCategoryDTO>> list(@ApiParam("父节点id") @RequestParam(required = false) String parentId) {
        return curriculumCategoryService.list(parentId);
    }

    @ApiOperation(value = "分页查询指定父节点id下的所有分类", notes = "parentId = null 查询所有分类，parentId = root 查询顶级分类")
    @GetMapping(value = "/page")
    public ActionResult<Pagination<CurriculumCategoryDTO>> get(@ApiParam("父节点id") @RequestParam(required = false) String parentId,
                                                               @ApiParam("页码") @RequestParam Integer pageNo,
                                                               @ApiParam("大小") @RequestParam Integer pageSize) {
        if (pageNo != null && pageNo <= 0)
            pageNo = 1;
        if (pageSize != null && pageSize <= 1)
            pageSize = 10;
        return curriculumCategoryService.page(parentId, pageNo, pageSize);
    }

    @ApiOperation(value = "刷新指定分类下的子分类排序", notes = "顶级节点id = root")
    @PutMapping("/refresh/{id}")
    public ActionResult<String> refresh(@ApiParam("id") @PathVariable String id) {
        return curriculumCategoryService.refresh(id);
    }

    @ApiOperation("获取所有分类")
    @GetMapping("/listAll")
    public ActionResult<List<CurriculumCategoryDTO>> listAll() {
        return curriculumCategoryService.listAll();
    }
}
